package com.dkd.manage.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.dkd.common.constant.DkdContants;
import com.dkd.common.utils.DateUtils;
import com.dkd.common.utils.uuid.UUIDUtils;
import com.dkd.manage.domain.po.Channel;
import com.dkd.manage.domain.po.Node;
import com.dkd.manage.domain.po.VendingMachine;
import com.dkd.manage.domain.po.VmType;
import com.dkd.manage.mapper.ChannelMapper;
import com.dkd.manage.mapper.NodeMapper;
import com.dkd.manage.mapper.VmTypeMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.dkd.manage.mapper.VendingMachineMapper;
import com.dkd.manage.service.IVendingMachineService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 设备管理Service业务层处理
 *
 * @author ming
 * @date 2024-10-26
 */
@Service
@RequiredArgsConstructor(onConstructor_ = {@Autowired})
public class VendingMachineServiceImpl implements IVendingMachineService {
    private final VendingMachineMapper vendingMachineMapper;

    private final NodeMapper nodeMapper;

    private final VmTypeMapper vmTypeMapper;

    private final ChannelMapper channelMapper;

    /**
     * 查询设备管理
     *
     * @param id 设备管理主键
     * @return 设备管理
     */
    @Override
    public VendingMachine selectVendingMachineById(Long id) {
        return vendingMachineMapper.selectVendingMachineById(id);
    }

    /**
     * 查询设备管理列表
     *
     * @param vendingMachine 设备管理
     * @return 设备管理
     */
    @Override
    public List<VendingMachine> selectVendingMachineList(VendingMachine vendingMachine) {
        return vendingMachineMapper.selectVendingMachineList(vendingMachine);
    }

    /**
     * 新增设备管理
     *
     * @param vendingMachine 设备管理
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertVendingMachine(VendingMachine vendingMachine) {
        // 生成设备编号
        String innerCode = UUIDUtils.getUUID();
        vendingMachine.setInnerCode(innerCode);
        // 查询售货机类型的容量
        VmType vmType = vmTypeMapper.selectVmTypeById(vendingMachine.getVmTypeId());
        vendingMachine.setChannelMaxCapacity(vmType.getChannelMaxCapacity());
        // 查询点位信息
        Node node = nodeMapper.selectNodeById(vendingMachine.getNodeId());
        vendingMachine.setAddr(node.getAddress());
        vendingMachine.setBusinessType(node.getBusinessType());
        vendingMachine.setRegionId(node.getRegionId());
        vendingMachine.setPartnerId(node.getPartnerId());
        // 设置设备状态
        vendingMachine.setVmStatus(DkdContants.VM_STATUS_NODEPLOY); // 0-表示未投放
        vendingMachine.setCreateTime(DateUtils.getNowDate());

        int inserted = vendingMachineMapper.insertVendingMachine(vendingMachine);

        List<Channel> channels = new ArrayList<>();
        for (int i = 1; i <= vmType.getVmRow(); i++) {
            for (int j = 1; j <= vmType.getVmCol(); j++) {
                Channel channel = Channel.builder()
                        .channelCode(i + "-" + j) // 货道编号
                        .innerCode(vendingMachine.getInnerCode()) // 售货机编号
                        .vmId(vendingMachine.getId()) // 售货机id
                        .maxCapacity(vendingMachine.getChannelMaxCapacity()) // 当前货道最大容量
                        .build();
                channel.setCreateTime(DateUtils.getNowDate());
                channels.add(channel);
            }
        }
        int batch = channelMapper.insertBatchChannel(channels);
        return inserted;
    }

    /**
     * 修改设备管理
     *
     * @param vendingMachine 设备管理
     * @return 结果
     */
    @Override
    public int updateVendingMachine(VendingMachine vendingMachine) {
        Node node = nodeMapper.selectNodeById(vendingMachine.getNodeId());
        vendingMachine.setPartnerId(node.getPartnerId());
        vendingMachine.setRegionId(node.getRegionId());
        vendingMachine.setBusinessType(node.getBusinessType());
        vendingMachine.setAddr(node.getAddress());

        vendingMachine.setUpdateTime(DateUtils.getNowDate());
        return vendingMachineMapper.updateVendingMachine(vendingMachine);
    }

    /**
     * 批量删除设备管理
     *
     * @param ids 需要删除的设备管理主键
     * @return 结果
     */
    @Override
    public int deleteVendingMachineByIds(Long[] ids) {
        return vendingMachineMapper.deleteVendingMachineByIds(ids);
    }

    /**
     * 删除设备管理信息
     *
     * @param id 设备管理主键
     * @return 结果
     */
    @Override
    public int deleteVendingMachineById(Long id) {
        return vendingMachineMapper.deleteVendingMachineById(id);
    }
}
